package com.tim.helper;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;

import javax.script.*;

/**
 * http工具类，回显数据
 */
public class HttpHelper2 {

    String prePath = "";
    int isGet = 0;// 0-POST 1-GET

    @Test
    public void send() throws ScriptException {
        useClearDev();
        isGet = 0;
        String path = "";
        String reqBody = "";// json对象

        doSend(path, reqBody);

        for (int i = 0; i < 3; i++) {
            JSONObject json = JSON.parseObject(reqBody);
            json.put("", "");
            doSend(path, JSON.toJSONString(json));
        }
    }

    private void doSend(String path, String reqBody) {
        // 执行请求
        String url = prePath + path;
        Method method = isGet==1 ? Method.GET : Method.POST;
        HttpRequest request = HttpUtil.createRequest(method, url)
                .header("contentType", "application/json")
                .header("token", "")
                .body(reqBody);
        String respBody = request.execute().body();

        System.out.println(respBody);
    }

    private void useClearLocal(){
        prePath = "localhost:8080/";
    }

    private void useClearDev(){
        prePath = "localhost:8080/";
    }

}
